package com.yh.threadTest.test;

/**
 * @ClassName Demo4
 * @Description TODO
 * @Author 0715-YuHao
 * @Date 2020/11/11 14:26
 */
public class Demo4 {

    public static void main(String[] args) {
        Runnable run = new Ticket();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();
    }

    public static class Ticket implements Runnable {

        private int count = 10;

        @Override
        public void run() {
            while (sale()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public synchronized boolean sale() {
            if (count > 0) {
                System.out.println(Thread.currentThread().getName() + ":正在卖票");
                count--;
                System.out.println("卖票成功，余票：" + count);
                return true;
            }
            return false;
        }
    }
}
